package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDeviceInfo;
import com.ruoyi.common.core.domain.entity.SysDeviceType;
import com.ruoyi.common.pojo.QueryDeviceListReq;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDeviceInfoMapper;
import com.ruoyi.system.mapper.SysDeviceTypeMapper;
import com.ruoyi.system.service.SysDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author syj
* @description 针对表【sys_device_info(设备信息表)】的数据库操作Service实现
* @createDate 2022-07-22 11:06:59
*/
@Service
public class SysDeviceInfoServiceImpl extends ServiceImpl<SysDeviceInfoMapper, SysDeviceInfo> implements SysDeviceInfoService {

    @Autowired
    SysDeviceTypeMapper sysDeviceTypeMapper;

    @Autowired
    SysDeviceInfoMapper sysDeviceInfoMapper;

    @Override
    public AjaxResult queryDeviceType() {
        List<SysDeviceType> sysDeviceTypes = sysDeviceTypeMapper.selectList(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getState, 1));
        return AjaxResult.success(sysDeviceTypes);
    }

    @Override
    public AjaxResult addDeviceType(SysDeviceType param) {
        param.setState(1);
        Date nowDate = DateUtils.getNowDate();
        param.setCreatedTime(nowDate);
        param.setUpdatedTime(nowDate);
        sysDeviceTypeMapper.insert(param);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteDeviceType(SysDeviceType param) {
        SysDeviceType sysDeviceType = new SysDeviceType();
        sysDeviceType.setId(param.getId());
        sysDeviceType.setState(2);
        sysDeviceTypeMapper.updateById(sysDeviceType);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateDeviceType(SysDeviceType param) {
        sysDeviceTypeMapper.updateById(param);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult queryDeviceList(QueryDeviceListReq param) {
        List<SysDeviceInfo> sysDeviceInfos = sysDeviceInfoMapper.selectList(Wrappers.<SysDeviceInfo>lambdaQuery().eq(SysDeviceInfo::getState, 1).eq(Objects.nonNull(param.getDeviceType()), SysDeviceInfo::getDeviceType, param.getDeviceType()));
        for (SysDeviceInfo sysDeviceInfo : sysDeviceInfos) {
            SysDeviceType sysDeviceType = sysDeviceTypeMapper.selectOne(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getDeviceType, sysDeviceInfo.getDeviceType()));
            sysDeviceInfo.setDeviceTypeName(sysDeviceType.getName());
        }
        return AjaxResult.success(sysDeviceInfos);
    }

    @Override
    public AjaxResult addDeviceInfo(SysDeviceInfo param) {
        param.setState(1);
        Date nowDate = DateUtils.getNowDate();
        param.setCreatedTime(nowDate);
        param.setUpdatedTime(nowDate);
        sysDeviceInfoMapper.insert(param);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteDeviceInfo(SysDeviceInfo param) {
        SysDeviceInfo sysDeviceInfo = new SysDeviceInfo();
        sysDeviceInfo.setId(param.getId());
        sysDeviceInfo.setState(2);
        sysDeviceInfoMapper.updateById(sysDeviceInfo);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateDeviceInfo(SysDeviceInfo param) {
        sysDeviceInfoMapper.updateById(param);
        return AjaxResult.success();
    }
}




